﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Ignitron.EEngine;
using Ignitron.EEngine.Maths;
using Ignitron.EEngine.Model;
using GameEngine;

namespace Board3D.Models
{
    /// <summary>
    /// Class representing piece model
    /// </summary>
    internal class ManModel: Model
    {
        // file names of textures and models
        private const string _sManModel = "kamen.x";
        private const string _sWhiteTexture = "kamen.white.jpg";
        private const string _sWhiteSelectedTexture = "kamen.white.selection.jpg";
        private const string _sBlackTexture = "kamen.black.jpg";
        private const string _sBlackSelectedTexture = "kamen.black.selection.jpg";

        // bitmaps
        private ETexture _bitmapWhite = null;
        private ETexture _bitmapBlack = null;
        private ETexture _bitmapWhiteSelected = null;
        private ETexture _bitmapBlackSelected = null;

        public ManModel()
        {
        }

        /// <summary>
        /// Loads model and textures for a man
        /// </summary>
        public void LoadModels(Engine engine, string sModelPath, string sPicturePath)
        {
            // load board model
            string sFile = Path.Combine(sModelPath, _sManModel);
            base.Load(engine, sFile);

            // load textures
            _bitmapWhite = engine.CreateTexture();
            _bitmapWhite.Load(Path.Combine(sPicturePath, _sWhiteTexture));

            _bitmapBlack = engine.CreateTexture();
            _bitmapBlack.Load(Path.Combine(sPicturePath, _sBlackTexture));

            _bitmapWhiteSelected = engine.CreateTexture();
            _bitmapWhiteSelected.Load(Path.Combine(sPicturePath, _sWhiteSelectedTexture));

            _bitmapBlackSelected = engine.CreateTexture();
            _bitmapBlackSelected.Load(Path.Combine(sPicturePath, _sBlackSelectedTexture));
            
            mTextureLoaded = true;

            //FlipTextureX();

            SetTransformMatrix(GetPrimaryTransformMatrix());
        }

        /// <summary>
        /// Return model transformation matrix
        /// </summary>
        public override EMatrix4 GetPrimaryTransformMatrix()
        {
            return EMatrix4.CreateRotationX((float)Math.PI) * EMatrix4.CreateScale(0.1f);
        }

        /// <summary>
        /// Renders the man with given parametres
        /// </summary>
        public void Render(IEngine engine, enumFigureColor figureColor, bool bSelected, int w, int h)
        {
            // set approprite texture
            if (bSelected == true)
            {
                mTexture = figureColor == enumFigureColor.White ? _bitmapWhiteSelected : _bitmapBlackSelected;
            }
            else
            {
                mTexture = figureColor == enumFigureColor.White ? _bitmapWhite : _bitmapBlack;
            }

            // call primary rendering routine
            base.Render(engine, w, h);           
        }
    }
}
